package com.tfswx.jkgl.core.utils;

import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author ricky
 * @date 2021/12/5
 */
public class ResourceUtil {
    private ResourceUtil() {
    }

    public static byte[] read(String path) {
        ClassPathResource classPathResource = new ClassPathResource(path);
        try (InputStream inputStream = classPathResource.getInputStream();
             ByteArrayOutputStream bos = new ByteArrayOutputStream(inputStream.available())) {
            byte[] buff = new byte[2048];
            int len;
            while ((len = inputStream.read(buff)) > 0) {
                bos.write(buff, 0, len);
            }
            byte[] bytes = bos.toByteArray();
            bos.flush();
            return bytes;
        } catch (IOException e) {
            throw new RuntimeException("资源读取失败", e);
        }
    }
}
